<template>
    <div>
        <basic-container>
            <avue-crud
                    :data="list"
                    :option="option"
                    ref="crud"
                    v-model="form"
                    @on-load="getList"
                    :table-loading="listLoading"
                    :page="page"
                    :permission="permission"
                    @search-change="handleFilter"
                    @refresh-change="handleRefreshChange"
                    @row-save="save"
            >
                <template slot-scope="scope"
                          slot="menuBtn">
                    <el-dropdown-item divided
                                      v-if="permissions.sys_activiti_model_edit"
                                      @click.native="handleView(scope.row)">模型图
                    </el-dropdown-item>
                    <el-dropdown-item divided
                                      v-if="permissions.sys_activiti_model_deploy"
                                      @click.native="handleDeploy(scope.row,scope.index)">部署
                    </el-dropdown-item>
                    <el-dropdown-item divided
                                      v-if="permissions.sys_activiti_model_del"
                                      @click.native="deletes(scope.row,scope.index)">删除
                    </el-dropdown-item>
                </template>
            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
    import {listPage,addModel,deleteModel,deployModel} from "@/api/activiti/model";
    import {tableOption} from '@/const/activiti/model';
    import {mapGetters} from "vuex";

    export default {
        data() {
            return {
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 10, // 每页显示多少条,
                    isAsc: false//是否倒序
                },
                permission:{},
                listLoading: false,
                list: [],
                option: tableOption,
                form: {},
            }
        },
        computed: {
            ...mapGetters(["permissions"])
        },
        created() {
            this.permission = {
                viewBtn: this.permissions['sys_activiti_model_view']?true:false,
                addBtn: this.permissions['sys_activiti_model_add']?true:false,
                delBtn: this.permissions['sys_activiti_model_del']?true:false,
                editBtn: this.permissions['sys_activiti_model_edit']?true:false,
                searchBtn: this.permissions['sys_activiti_model_view']?true:false,
            }
        },
        methods: {
            //获取分页数据
            getList(page, params) {
                this.listLoading = true;
                listPage(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.list = response.data.data.records
                    this.page.total = response.data.data.total
                    this.listLoading = false;
                })
            },
            save(row, done,loading) {
                addModel(this.form).then(() => {
                    this.getList(this.page);
                    done();
                    this.$notify({
                        title: "成功",
                        message: "创建成功",
                        type: "success",
                        duration: 2000
                    });
                }).catch(() => {
                    loading();
                });
            },
            handleView(row){
                const name = `模型id为${row.id}的${row.name}流程图`,
                    src = `/activti/detail/${row.id}`;
                this.$router.push({
                    path: src,
                    query: {
                        name: name
                    }
                })
            },
            handleDeploy: function (row) {
                var _this = this
                this.$confirm('是否确认部署ID为"' + row.id + '"的模型?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return deployModel(row.id)
                }).then((response) => {
                    console.log(response.data)
                    if(response.data.code =='0'){
                        _this.$message({
                            showClose: true,
                            message: '部署成功',
                            type: 'success'
                        })
                    }else{
                        _this.$message({
                            showClose: true,
                            message: response.data.msg,
                            type: "error",
                        })
                    }
                    this.getList(this.page)
                }).catch(function () {
                })
            },
            deletes(row,index){
                this.$confirm(
                    "此操作将永久删除, 是否继续?",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }
                ).then(() =>{
                    deleteModel(row.id).then(() =>{
                        this.list.splice(index, 1);
                        this.$notify({
                            title: "成功",
                            message: "删除成功",
                            type: "success",
                            duration: 2000
                        });
                        this.getList(this.page);
                    });
                })
            },
            handleFilter(param) {
                this.page.page = 1;
                this.getList(this.page, param);
            },
            handleRefreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
